<?php namespace Admin\Controller;
//商品控制器
class GoodsController extends CommonController{
	private $model;
	public function __auto(){
		$this->model = new \Common\Model\Goods;
	}
	
	//商品列表
	public function index(){
		$data=$this->model->get();
		if(!$data){
			View::success('请先添加类型',U('add'));
		}
		View::with('data',$data);
		View::make();
	}
	
	//获得类型属性
	public function getAttr(){
		$tid = Q('post.t',0,'intval');
		$typeAttr = new \Common\Model\TypeAttr;
		$data = $typeAttr->where("type_tid={$tid}")->get();
//		p($data);exit;
		foreach ($data as $k=>$v){
			$data[$k]['tavalue']=explode('|',$v['tavalue']);
		}
		echo json_encode($data);
	}

	//商品添加
	public function add(){
		$cate = new \Common\Model\CateGory;
		$cateData = $cate->get();
		$brand = new \Common\Model\Brand;
		$brandData = $brand->get();
		if(IS_POST){
			if($this->model->store()){
				View::success("添加成功",U('index'));
			}
			View::error($this->model->getError());
		}
		View::with('cateData',$cateData);
		View::with('brandData',$brandData);
		View::make();
	}
	
	//商品编辑
	public function edit(){
		if(IS_POST){
			if($this->model->edit()){
				View::success("编辑成功",U('index'));
			}
			View::error($this->model->getError());
		}
		//获得旧数据
		$gid=Q('get.gid',0,'intval');
		$goods=new \Common\Model\Goods;
		$oldData=$goods->join('details','goods.gid','=','gid')
					   ->where("goods.gid={$gid}")->find();
//		p($oldData);exit;
		View::with('oldData',$oldData);
		//处理所属分类
		$cate=new \Common\Model\CateGory;
		$cateData=$cate->getAll();
//		p($cateData);exit;
		View::with('cateData',$cateData);
		//处理所属品牌
		$brand = new \Common\Model\Brand;
		$brandData = $brand->get();
//		p($brandData);exit;
		View::with('brandData',$brandData);
		//处理商品详情
		$details = new \Common\Model\Details;
		$detailsData=$details->where("goods_gid={$gid}")->find();
//		p($detailsData);exit;
		View::with('detailsData',$detailsData);
		//处理商品属性和规格
		$goodsAttr=new \Common\Model\GoodsAttr;
		$data=$goodsAttr->join('typeattr','typeattr_taid','=','taid')
						->where("goods_gid={$gid}")->get();
//		p($data);exit;
		//获得taid为排除已经选过的类型属性
		$taid=[];
		foreach ($data as $k=>$v){
			$data[$k]['tavalue']=explode('|',$v['tavalue']);
			$taid[]=$v['taid'];
		}
//		p($taid);exit;
		//获取商品没有选过的属性
		$typeAttr=new \Common\Model\TypeAttr;
		$taidStr=implode(',',$taid);
		$type=$typeAttr->where("taid NOT IN ({$taidStr}) AND type_tid={$oldData['type_tid']}")->get();
//		p($type);exit;
		foreach ($type as $v){
			$data[]=array(
				'gtvalue'=>'',
				'taname'=>$v['taname'],
				'tavalue'=>explode('|',$v['tavalue']),
				'class'=>$v['class'],
				'taid'=>$v['taid'],
			);
		}
//		p($data);exit;
		View::with('data',$data);
		View::make();
	}
	
	//商品删除
	public function del(){
		$gid=Q('get.gid',0,'intval');
		$data=$this->model->where("gid={$gid}")->find();
		Db::table('goods_attr')->where("goods_gid={$gid}")->delete();
		Db::table('details')->where("goods_gid={$gid}")->delete();
		Db::table('goods_list')->where("goods_gid={$gid}")->delete();
		$this->model->where("gid={$gid}")->delete();
		View::success("删除成功",U('index'));
	}
	
	//uploadify异步上传
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	
}